﻿using System;
using System.Configuration;
using System.ServiceModel;
using RentalOfCars.Server.Domain.ClientServerInterface;
using RentalOfCars.Server.Infrastructure.ClientServerInterface;
using RentalOfCars.Server.Infrastructure.Database;

namespace RentalOfCars.Server.Application
{
    internal class Program
    {
        private static void Main()
        {
            string serverUri = ConfigurationManager.AppSettings.Get("ServerUri");
            var baseUri = new Uri(serverUri);

            string dataSource = ConfigurationManager.AppSettings.Get("DbServer");
            string user = ConfigurationManager.AppSettings.Get("DbUser");
            string password = ConfigurationManager.AppSettings.Get("DbPassword");
            string connectionString = string.Format("Data Source={0};User Id={1};Password={2}", dataSource, user, password);

            
            var accessor = OracleAccessor.GetInstance(connectionString);
            accessor.Connect();

            //var accessor = new FakeAccessor();
            var selfHost = new CarsServiceHost(accessor, typeof (DbAccessSession), baseUri);

            try
            {
                selfHost.AddServiceEndpoint(typeof (IDbAccessSession), new NetTcpBinding(), serverUri);

                selfHost.Open();
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();

                selfHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }
        }
    }
}